home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6570 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.4 KB  |  58 lines

  1. Path: kryten.awinc.com!news
  2. From: jenna.design@awinc.com (jenna design)
  3. Newsgroups: comp.lang.c,comp.lang.c++
  4. Subject: Re: How to the size of a file in C ?
  5. Date: 9 Feb 1996 16:18:56 GMT
  6. Organization: A & W Internet Inc.
  7. Message-ID: <4ffs5g$a71@kryten.awinc.com>
  8. References: <4ffeqa$pjh@brtph500.bnr.ca>
  9. NNTP-Posting-Host: pme004.awinc.com
  10. Mime-Version: 1.0
  11. Content-Type: Text/Plain; charset=ISO-8859-1
  12. X-Newsreader: WinVN 0.99.5
  13.  
  14. In article <4ffeqa$pjh@brtph500.bnr.ca>, gilbertb@bnr.ca says...
  15. >
  16. >How do I get the size of a file in C UNIX-based?  I tried sizeof <file>, but 
  17. >that only returns the size of the object called "<file>" and not the data.
  18. >I know about filelength(), also, but that is DOS, Windows, and OS/2.
  19. >
  20. >Can someone help me? Post a reply, my email is corrupted.
  21. >-- 
  22. >Gilbert Banks
  23. >
  24.  
  25. Try this function Gilbert.
  26.  
  27. /*************************************************
  28. * This function determines the size of a file
  29. * Returns a long.  Cast if you need to.
  30. * A return of 0 means failure else it returns number 
  31. * of bytes read.
  32. * Your code should provide a error handling routine  
  33. * if 0 is returned to the calling function.
  34. * Calling function must pass a pointer to \path\filename
  35. * details of the file.
  36. */
  37. #include <stdio.h>
  38.  
  39. long GetFileSize (char *ptr_fName)
  40. {
  41.  FILE *fp;
  42.  long f_size;
  43.  
  44.  if((fp = fopen(ptr_fName,"r"))==NULL)
  45.     return 0;
  46.  
  47.  fseek(fp, 0L, SEEK_END);
  48.  f_size = ftell(fp);
  49.  
  50.  fclose(fp);
  51.  
  52.  return (f_size);
  53. }
  54.  
  55. /* Good day */
  56.  
  57.